<?php
class ImagesController extends AppController {

	var $name = 'Images';
	var $components = array('Uploader.Uploader');
	function beforeFilter() {
        parent::beforeFilter(); 
		$this->Uploader->uploadDir = 'files/uploads/images/'; //thu muc chua file upload
        $this->Uploader->enableUpload = true;
        $this->Uploader->maxFileSize = '5M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
        $this->Uploader->maxNameLength = 25;//do dai cua ten file
        $this->Uploader->tempDir = TMP;       
    }

	function index() {
		$this->Image->recursive = 0;
		$this->set('images', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid image', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('image', $this->Image->read(null, $id));
	}

	
	function admin_view($gid = null) {		
		$this->Image->recursive = 1;		
		$conditions = array('Image.plans_id' => $gid);
 		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'Image.id ASC'
		); 	
		$this->set('images', $this->paginate());
		//$galleries = $this->Image->Plan->find('list');
		$choose = $this->Image->Plan->find('first',array('conditions'=>array('Plan.id'=>$gid)));
  		$this->set(compact('gid','choose'));
		//debug( $this->paginate());
	}

	function admin_add() {
		//$this->Image->recursive = 1;	
		$this->loadModel('Plan');
		$plan = $this->Plan->find('first',array('conditions'=>array('id'=>$this->data['Images']['plans_id'])));	
		//debug($plan);
		// sửa lại những ảnh đã có và có status = 1
		if(!empty($plan['Image'])){
			foreach($plan['Image'] as $img){
				if($img['status']==1){
					$this->Image->id = $img['id'];
					$this->Image->saveField('status',0);
				}
			}
		}
		$data = array();
			if(!empty($this->data)){				
				$upload = $this->Uploader->uploadAll(array('url1','url2','url3','url4','url5'));	
				$data['Image']['plans_id'] = $this->data['Images']['plans_id'];						
				$j = 0;
				for($i=1;$i<6;$i++){					
					if(isset($upload['url'.$i])){
						$data['Image']['url'] =  $upload['url'.$i]['name'];								
						$data['Image']['note'] = $this->data['Images']['note'.$i];
						if($this->data['Images']['status']==$i) $data['Image']['status'] = 1;
						else $data['Image']['status'] = 0;
						$this->Image->create();
						if ($this->Image->save($data)){	
							$j++;
							//$this->Session->setFlash(__('Đã lưu.', true));					
							//debug($data);
						} else {
							$this->Session->setFlash(__('Có lỗi xảy ra. Hãy thử lại!', true));
						}						
					}					
				}
				$this->Session->setFlash(__('Đã thêm '.$j.' ảnh', true));
				$this->redirect(array('action' => 'view',$data['Image']['plans_id']));
				
			}					
		    
		$plans = $this->Image->Plan->find('list');
		$this->set(compact('plans'));
		//debug($galleries);
		
	}	
	function _delimg($id = null){
			$files = $this->Image->find('list',array(
				'conditions'=>array('Image.id'=>$id),
				'fields'=>'url')
			);
			$file ='';	
			$file = "files" . DS . "uploads" . DS. "images" . DS .$files[$id];
			if(is_file($file)){
				unlink($file);		
			}
	}
	function admin_delete($gid=NULL, $id = NULL) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại ảnh này !', true));
			$this->redirect(array('action'=>'view',$gid));
		}
		$this->_delimg($id);
		if ($this->Image->delete($id)) {
			$this->Session->setFlash(__('Đã xóa !', true));
			$this->redirect(array('action'=>'view',$gid));
		}
		$this->Session->setFlash(__('Có lỗi sảy ra. Xóa không thành công !', true));
		$this->redirect(array('action' => 'view',$gid));
	}
	
	function admin_choose($news, $old){
		$this->render(false);
		$this->Image->id = $news;
		$this->Image->saveField('status',1);
		$this->Image->id = $old;
		$this->Image->saveField('status',0);
	}
}
